package cn.codelab.chatbot.api.controller;

import cn.codelab.chatbot.domain.chatgptapi.IChatgptApi;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author : chenzhenlin
 * @version :
 * @className : ChatApiController
 * @date : Created in 2023/2/16 15:03
 * @description:
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/chat")
public class ChatApiController {
    @Value("${chat.api.key}")
    private String openAiKey;

    private final IChatgptApi chatgptApi;

    @GetMapping("/ask")
    public String doChatAnswer(String question) throws IOException {
        log.info("提问：{}", question);
        String answer = chatgptApi.doChatGPT(openAiKey, question);
        log.info("ChatGPT回答：{}", answer);
        return answer;
    }
}
